//*************************************************
// Filename: create_team.js
// 
// This JavaScript file contains a function for
// checking and validating the fields contained
// in the create_team.php page.
//
//
// Author: Caleb Goff
// Date Created: 10/10/2009
//*************************************************

/**
 * @Desc Function called on a POST.
 *
 */
function postLoad(){
	resetElems();
}

/**
 * @Desc Resets all of the elements with the class name 'resets'.
 */
function resetElems(){

	document.getElementById("league_id").value = "NULL";
	var elems = document.getElementsByClassName("resets");

	for (var i = 0; i < elems.length; i++)
		elems[i].value = "";

}

/**
* @Desc Checks and validates the create team form fields.
*
**/
function checkCreateTeamFields() {

	var league_id = document.getElementById("league_id").value;
	var league_pwd = document.getElementById("league_password").value;
	var team_name = document.getElementById("team_name").value;
	var team_pwd = document.getElementById("team_password").value;
	var team_pwd_confirm = document.getElementById("team_password_confirm").value;
	var team_email = document.getElementById("team_email").value;
	var team_question = document.getElementById("team_question").value;
	var team_answer = document.getElementById("team_answer").value;
    var upload_image_radio = document.upload_form.upload_image;
    var upload = '';

	//Input checks
    if(checkInput(league_pwd) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    if(checkInput(team_name) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    if(checkInput(team_pwd) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    if(checkInput(team_pwd_confirm) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    if(checkInput(team_email) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }
    if(checkInput(team_question) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }   
    if(checkInput(team_answer) == false){
		Popup.showModal('popupInvaludInput',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;    
    }         
                  
    
    // Check password length
	if (league_pwd.length < 6) {
		Popup.showModal('popupBadLeaguePwd',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	} else {
		league_pwd = getHash(document.getElementById("league_password"));
	}
	
    // Check team name length
	if (team_name.length < 4) {
		Popup.showModal('popupBadTeamName',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}
	
    // Check password length
	if (team_pwd.length < 6) {
		Popup.showModal('popupBadTeamPwd',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}

    // Check if confirm passwords match
	if (!(team_pwd_confirm == team_pwd)) {
		Popup.showModal('popupBadPwdMatch',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	} 
	else {
		team_pwd = getHash(document.getElementById("team_password"));
	}

    // Check email length and validate it
	if (team_email.length < 10 || echeck(team_email) == false) {
		Popup.showModal('popupBadEmail',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
		return;
	}

    // Check upload value from radio buttons
	for (i=0; i < upload_image_radio.length; i++) {
		if (upload_image_radio[i].checked == true) {
			upload = upload_image_radio[i].value;
		}
	}
	// End input checks

    // Grab team verification key
	document.getElementById("team_verify").value = team_name + league_id;
	var key = getHash(document.getElementById("team_verify"));

	//Send these login paramaters to the PHP function on the server using
	//an HTTP post request
	var paramNames = "leagueId,leaguePwd,teamName,teamPwd,teamEmail,key,teamQuestion,teamAnswer,uploadStatus";
	var paramVals  = league_id + "," + league_pwd + "," + team_name + "," + team_pwd + "," + team_email + "," + key + "," + team_question 
                     + "," + team_answer + "," + upload;
	var url        = "create_team.php";
	
	//Wait for a response from the login.php file
	var response = postRequest(paramNames, paramVals, url);
	
	// Check if login was successful
	switch ((response-0)) {
		case 1:
		{
            // If team name already exists
			Popup.showModal('popupTeamExists',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
		case 2:
		{
            // If email is already registered to a team
			Popup.showModal('popupEmailExists',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
		case 3:
		{
            // Invalid league selected
			Popup.showModal('popupBadLeague',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
		case 4:
		{
            // Invalid league password provided
			Popup.showModal('popupBadLeaguePwd',null,null,{'screenColor':'#C0C0C0','screenOpacity':.6});
			break;
		}
        case 99:
        {
            // Redirect to image upload
            window.location = "upload_image.php?email="+team_email+"&league="+league_id;
            break;
        }
		default:
		{
            // Success
            window.location = "index.php";
			break;
		}
	}
}
